package com.pioneer.swing.controller;

import com.pioneer.dao.*;
import com.pioneer.beans.Manifest;
import com.pioneer.beans.Container;
import com.pioneer.beans.ContainerContent;
import com.pioneer.dao.DataAccessException;
import com.pioneer.swing.ui.*;

import com.pioneer.swing.model.AllContainersTable;
import com.pioneer.swing.model.AllManifestsTable;

import java.awt.Window;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JInternalFrame;
import javax.swing.event.InternalFrameAdapter;
import javax.swing.event.InternalFrameEvent;
import javax.swing.table.TableModel;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;

/**
 * Title:        Pioneer Metals
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:      Pioneer Metals
 * @author Gary Chen
 * @version 1.0
 */

public class WindowCloser extends InternalFrameAdapter {

	private ManifestDAO manifestDAO;
	private JobDAO jobDAO;
	private ContactDAO contactDAO;

    public WindowCloser() {		
    }
	
	public void setManifestDAO(ManifestDAO manifestDAO) {
		this.manifestDAO=manifestDAO;
	}
	
	public void setContactDAO(ContactDAO contactDAO) {
		this.contactDAO = contactDAO;
	}
	
	public void setJobDAO(JobDAO jobDAO) {
		this.jobDAO = jobDAO;
	}
	
    public static void main(String[] args) {
        //WindowCloser windowCloser1 = new WindowCloser();
    }


	public void internalFrameClosing(InternalFrameEvent e)
	{
		JInternalFrame frame = e.getInternalFrame();		
		String windowName=frame.getName();
		if (windowName.equals(UIConstants.TITLE_CONTAINER_ENTRY)) {
			handleContainerEntryForm(frame);
		} else if (windowName.equals(UIConstants.TITLE_MANIFEST_ENTRY)) {
			handleManifestEntryForm(frame);
		} else if (windowName.equals(UIConstants.TITLE_JOB_ENTRY)) {
			handleJobEntryForm(frame);
		}
	}
	
	private void handleJobEntryForm(JInternalFrame window) {
		JobEntryFrame jobEntryFrame=(JobEntryFrame)window;
		int returnInt = JOptionPane.showConfirmDialog(jobEntryFrame,
			UIConstants.MESG_SAVE_CONFIRM,
			UIConstants.TITLE_SAVE_CONFIRM,
			JOptionPane.YES_NO_CANCEL_OPTION);
		switch (returnInt) {
			case JOptionPane.YES_OPTION:
				ErrorCode errCode=Validator.validate(jobEntryFrame, jobDAO);
				if (errCode.getErrCode()!=ErrorCode.OK) {
					JOptionPane.showMessageDialog(jobEntryFrame, errCode.getErrMesg(),
				    UIConstants.TITLE_DATA_FORMAT, JOptionPane.INFORMATION_MESSAGE);
				}
				else {
					Saver saver = new Saver(jobEntryFrame);
					saver.setJobDAO(jobDAO);					
					saver.saveJob();
					window.dispose();
				}
				break;
			case JOptionPane.NO_OPTION:
				//System.out.println("Aborting saving");
				window.dispose();
		}
	}
	
	

	private void handleManifestEntryForm(JInternalFrame window) {
		ManifestEntryFrame manifestEntryFrame=(ManifestEntryFrame)window;
		int returnInt = JOptionPane.showConfirmDialog(manifestEntryFrame,
			UIConstants.MESG_SAVE_CONFIRM,
			UIConstants.TITLE_SAVE_CONFIRM,
			JOptionPane.YES_NO_CANCEL_OPTION);
		switch (returnInt) {
			case JOptionPane.YES_OPTION:
				ErrorCode errCode=Validator.validate(manifestEntryFrame, manifestDAO);
				if (errCode.getErrCode()!=ErrorCode.OK) {
					JOptionPane.showMessageDialog(manifestEntryFrame, errCode.getErrMesg(),
				    UIConstants.TITLE_DATA_FORMAT, JOptionPane.INFORMATION_MESSAGE);
				}
				else {
					Saver.saveManifest(manifestDAO,manifestEntryFrame);
					window.dispose();
				}
				break;
			case JOptionPane.NO_OPTION:
				//System.out.println("Aborting saving");
				window.dispose();
		}
	}

	private void handleContainerEntryForm(JInternalFrame window) {
		ContainerEntryFrame containerEntryFrame=(ContainerEntryFrame)window;
		int returnInt = JOptionPane.showConfirmDialog(containerEntryFrame,
			UIConstants.MESG_SAVE_CONFIRM,
			UIConstants.TITLE_SAVE_CONFIRM,
			JOptionPane.YES_NO_CANCEL_OPTION);
		switch (returnInt) {
			case JOptionPane.YES_OPTION:
				ErrorCode errCode=Validator.validate(containerEntryFrame, manifestDAO);
				if (errCode.getErrCode()!=ErrorCode.OK) {
					JOptionPane.showMessageDialog(containerEntryFrame, errCode.getErrMesg(),
				    UIConstants.TITLE_DATA_FORMAT, JOptionPane.INFORMATION_MESSAGE);
				}
				else {
					Saver.saveContainerEntry(manifestDAO,containerEntryFrame);
					window.dispose();
				}
				break;
			case JOptionPane.NO_OPTION:
				//System.out.println("Aborting saving");
				window.dispose();
		}
	}




}